package com.demo.myapplication.ui;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Shader;
import android.support.v4.content.ContextCompat;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.TextView;

import com.demo.myapplication.R;

public class CustomeText extends TextView {
  private final String Tag = CustomeText.class.getSimpleName();
  private Paint mPaint1;
  private Paint mPaint2;
  private int mViewWidth;
  private Paint mPaint;
  private LinearGradient mLinearGradient;
  private Matrix mGradientMatrix;
  private int mTranslate;

  private int outColor;
  private int innerColor;

  public CustomeText(Context context) {
    super(context);
    Log.d(Tag, "CustomeText(Context context)");
  }

  public CustomeText(Context context, AttributeSet attrs) {
    super(context, attrs);
    Log.d(Tag, "CustomeText(Context context, AttributeSet attrs)");
    init(context, attrs);
    mPaint1 = new Paint();
    mPaint1.setColor(outColor == 0 ? Color.YELLOW : outColor);
    mPaint1.setStyle(Paint.Style.FILL);
    mPaint2 = new Paint();
    if(innerColor == 0) {
      innerColor = ContextCompat.getColor(context, android.R.color.holo_blue_light);
    }
    mPaint2.setColor(innerColor);
    mPaint2.setStyle(Paint.Style.FILL);
  }

  private void init(Context context, AttributeSet attrs) {
    TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.CustomeText);
    outColor = array.getColor(R.styleable.CustomeText_OutBgColor, 0);
    innerColor = array.getColor(R.styleable.CustomeText_InnerBgColor, 0);
    array.recycle();
  }

  public CustomeText(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    init(context, attrs);
    Log.d(Tag, "CustomeText(Context context, AttributeSet attrs, int defStyleAttr)");

  }

  @Override
  protected void onSizeChanged(int w, int h, int oldw, int oldh) {
    super.onSizeChanged(w, h, oldw, oldh);
    if (mViewWidth == 0) {
      mViewWidth = getMeasuredWidth();
      if (mViewWidth > 0) {
        mPaint = getPaint();
        mLinearGradient = new LinearGradient(0, 0, mViewWidth, 0, new int[]{Color.BLUE, 0xffffffff, Color.YELLOW},
            null, Shader.TileMode.CLAMP);
        mPaint.setShader(mLinearGradient);
        mGradientMatrix = new Matrix();
      }
    }
  }

  @Override
  protected void onDraw(Canvas canvas) {
    canvas.drawRect(0, 0, getMeasuredWidth(), getMeasuredHeight(), mPaint1);
    canvas.drawRect(10, 10, getMeasuredWidth() - 10, getMeasuredHeight() - 10, mPaint2);
    canvas.save();
    super.onDraw(canvas);
    canvas.restore();
    if (mGradientMatrix != null) {
      mTranslate += mViewWidth / 5;
      if (mTranslate > 2 * mViewWidth) {
        mTranslate = -mViewWidth;
      }
      mGradientMatrix.setTranslate(mTranslate, 0);
      mLinearGradient.setLocalMatrix(mGradientMatrix);
      postInvalidateDelayed(100);
    }
  }
}
